import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import legacy from '@vitejs/plugin-legacy'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
// https://vitejs.dev/config/
const pathSrc = path.resolve(__dirname, 'fastCAE')
export default defineConfig({
	plugins: [
		legacy({
			targets: ['chrome 52'], // 需要兼容的目标列表，可以设置多个
      		additionalLegacyPolyfills: ['regenerator-runtime/runtime'], // 面向IE11时需要此插件
			//targets: ['defaults', 'not IE 11'],
		}),
		vue(),
		viteStaticCopy({
			targets: [
			  { src: 'node_modules/itk/WebWorkers/*', dest: 'fast/itk/WebWorkers' },
			  { src: 'node_modules/itk/PolyDataIOs/*', dest: 'fast/itk/PolyDataIOs' },
			]
		  }),
		  AutoImport({
			resolvers: [
			  ElementPlusResolver(),
			  IconsResolver({
				prefix: 'Icon'
			  })
			],
			dts: path.resolve(pathSrc + '/autoImport', 'auto-imports.d.ts')
		  }),
		  Components({
			resolvers: [
			  ElementPlusResolver(),
			  IconsResolver({
				enabledCollections: ['ep', 'carbon', 'noto']
			  })
			],
			dts: path.resolve(pathSrc + '/autoImport', 'components.d.ts')
		  }),
		  Icons({
			autoInstall: true,
			compiler: 'vue3'
		  })
	],
	
	server: {
		host: '0.0.0.0',
		hmr: true,
		open: '/fast/index.html',
		port: '6001',
		//https: true,
		//secure: false
		
	},
	base: './',
	build: {
		target: 'ES2015',
		base: './source/',
		minify: false,
		rollupOptions: {
			input:{
				//main: path.resolve(__dirname,'multiphysics/', 'index.html'),
				multiphysics: path.resolve(__dirname,'multiphysics/', 'index.html'),
				fast: path.resolve(__dirname, 'fast/','index.html'),
			},
			output: {
				// vite打包以后的js,css和img资源分别分门别类在js/css/img文件夹中
				entryFileNames: 'static/js/[name]-[hash].js',
				chunkFileNames: 'static/js/[name]-[hash].js',
				assetFileNames: 'static/[ext]/name-[hash].[ext]',
			}
		  }
		/* sourcemap: true,
		rollupOptions: {
		output: {
			sourcemap: true,
		},
		} */
	},
	resolve: {
		alias: {
			'@': fileURLToPath(new URL('./common', import.meta.url)),
			'paraview-glance': path.resolve(__dirname, './'),
			
		}
	},
	css: {
		postcss: {
			plugins: [
				{
					postcssPlugin: 'internal:charset-removal',
					AtRule: {
						charset: (atRule) => {
							if (atRule.name === 'charset') {
								atRule.remove()
							}
						},
					},
				},
			],
		},
	},
})
